package com.hetao.jmb2c.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hetao.jmb2c.common.PageResult;
import com.hetao.jmb2c.common.Result;
import com.hetao.jmb2c.dto.GoodsPageQueryDTO;
import com.hetao.jmb2c.entity.Goods;
import com.hetao.jmb2c.service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品信息管理控制器
 * TODO0710：已完成商品管理后台的搭建与分类中显示/隐藏逻辑的对接
 */
@Slf4j
@Tag(name = "商品信息管理", description = "商品信息管理")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @Operation(summary = "添加商品信息")
    @PostMapping("")
    public Result<Object> add(@RequestBody Goods goods){
        log.info("添加商品信息：{}", goods);
        this.goodsService.save(goods);
        return Result.success();
    }

    @Operation(summary = "修改商品信息")
    @PutMapping("")
    public Result<Object> update(@RequestBody Goods goods){
        log.info("修改商品信息：{}", goods);
        this.goodsService.updateById(goods);
        return Result.success();
    }

    @Operation(summary = "批量删除商品")
    @DeleteMapping("")
    public Result<Object> delete(@RequestBody List<Long> ids){
        log.info("删除商品，ids:{}", ids);
        this.goodsService.removeBatchByIds(ids);
        return Result.success();
    }

    @Operation(summary = "根据ID查询商品信息")
    @GetMapping("{id}")
    public Result<Goods> get(@PathVariable Long id){
        return Result.success(this.goodsService.getById(id));
    }

    @Operation(summary = "分页查询商品")
    @GetMapping("page")
    public PageResult<Goods> page(@ModelAttribute GoodsPageQueryDTO goodsPageQueryDTO){
        IPage<Goods> page= this.goodsService.getPage(goodsPageQueryDTO);
        return PageResult.success(page);
    }

    @Operation(summary = "修改商品状态（上架/下架）")
    @PutMapping("/status")
    public Result<Object> updateStatus(
            @Parameter(description = "商品ID", required = true) @RequestParam Long id,
            @Parameter(description = "状态：0-下架，1-上架", required = true) @RequestParam Integer status){
        log.info("修改商品状态：id={}, status={}", id, status);
        this.goodsService.updateStatus(id, status);
        return Result.success();
    }

    @Operation(summary = "修改商品显示状态（隐藏/显示）")
    @PutMapping("/visible")
    public Result<Object> updateVisible(
            @Parameter(description = "商品ID", required = true) @RequestParam Long id,
            @Parameter(description = "是否显示：0-隐藏，1-显示", required = true) @RequestParam Integer visible){
        log.info("修改商品显示状态：id={}, visible={}", id, visible);
        this.goodsService.updateVisible(id, visible);
        return Result.success();
    }
}
